package logic

import (
	"campus-trade/models/cache"
	"context"
	"fmt"
	"math/rand"
	"time"

	"campus-trade/campusrpc/campusrpc"
	"campus-trade/campusrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *campusrpc.SendSmsRequest) (*campusrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line

	i, _ := cache.Rdb.Get(l.ctx, "SendSmsNumber"+in.Mobile).Int()
	if i >= 1 {
		return nil, fmt.Errorf("同一手机号1分钟内仅允许发送1条验证码")
	}

	code := rand.Intn(9000) + 1000
	//sms, _ := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	//if sms.Code != 2 {
	//	return nil, fmt.Errorf(sms.Msg)
	//}
	cache.Rdb.Set(l.ctx, "SendSms"+in.Mobile, code, time.Minute*5)

	cache.Rdb.Incr(l.ctx, "sendSmsNumber"+in.Mobile)
	cache.Rdb.Expire(l.ctx, "SendSmsNumber"+in.Mobile, time.Minute*1)
	return &campusrpc.SendSmsResponse{}, nil
}
